package cn.kgc.libraryapplication.controller;

import cn.kgc.libraryapplication.service.UserService;
import cn.kgc.librarycommon.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/register")
    public Map<String, Object> register(User user){
        Map<String, Object> map = new HashMap<>();
        //校验用户名
        String username = user.getUsername();
        if(username == null || username == ""){
            map.put("result", -1);
            map.put("message", "用户名不能为空");
            return map;
        }
        if(isRepeat(username) != 0){
            map.put("result", -1);
            map.put("message", "用户名已存在");
            return map;
        }
        //校验密码
        String password = user.getPassword();
        if(password == null || password == ""){
            map.put("result", -2);
            map.put("message", "密码不能为空");
            return map;
        }
        if(user.getPassword().length() < 6){
            map.put("result", -2);
            map.put("message", "密码不能少于6位");
            return map;
        }
        //MD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        //校验姓名
        String name = user.getName();
        if(name == null || name == ""){
            map.put("result", -3);
            map.put("message", "姓名不能为空");
            return map;
        }
        //校验手机号
        String phone = user.getPhone();
        if (phone == null || !phone.matches("^1\\d{10}")) {
            map.put("result", -4);
            map.put("message", "手机号为空或不符合格式");
            return map;
        }
        //添加用户
        if(userService.addUser(user) > 0){
            map.put("result", 1);
            map.put("message", "注册成功，请登录系统");
        } else {
            map.put("result", 0);
            map.put("message", "系统繁忙，请稍后重试");
        }
        return map;
    }

    @RequestMapping("/isRepeat")
    public Integer isRepeat(String username){
        return userService.isRepeat(username);
    }

    @ResponseBody
    @RequestMapping("/login")
    public Map<String, Object> login(String username, String password, HttpSession session){
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Map<String, Object> map = new HashMap<>();
        User user = userService.login(username);
        if(user == null){
            map.put("result",-1);
            map.put("message","用户名不存在");
        } else if(!user.getPassword().equals(password)){
            map.put("result",-2);
            map.put("message","密码错误");
        } else {
            map.put("result",0);
            map.put("username", user.getName());
            session.setAttribute("user", user);
        }
        return map;
    }
}
